//
//  SLPOPDecayController.m
//  Objective-C
//
//  Created by 陆承东 on 2018/7/7.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLPOPDecayController.h"

#import <pop/POP.h>

@interface SLPOPDecayController ()

@property (nonatomic, weak) UIView *blueBallView;

@end

@implementation SLPOPDecayController

- (void)initNavBar {
    self.navTitle = @"POP DecayAnimation";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
}

- (void)initView {
    CGFloat blueBallViewWH = 60.0;
    UIView *blueBallView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, blueBallViewWH, blueBallViewWH)];
    self.blueBallView = blueBallView;
    blueBallView.backgroundColor = [UIColor blueColor];
    blueBallView.layer.cornerRadius = blueBallViewWH / 2.0;
    blueBallView.layer.masksToBounds = true;
    [self.view addSubview:blueBallView];
    
    UIPanGestureRecognizer *blueBallViewPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(blueBallViewPaned:)];
    [blueBallView addGestureRecognizer:blueBallViewPan];
}

- (void)blueBallViewPaned:(UIPanGestureRecognizer *)sender {
    CGPoint velocity = [sender velocityInView:self.view];
    NSLog(@"velocity: %@", NSStringFromCGPoint(velocity));
//    CGPoint translation = [sender translationInView:self.view];
//    NSLog(@"translation: %@", NSStringFromCGPoint(translation));
    
    POPDecayAnimation *decay = [POPDecayAnimation animationWithPropertyNamed:kPOPViewCenter];
    /**
     @abstract The current velocity value.
     @discussion Set before animation start to account for initial velocity.
                 Expressed in change of value units per second.
                 The only POPValueTypes supported for velocity are: kPOPValuePoint, kPOPValueInteger, kPOPValueFloat, kPOPValueRect and kPOPValueSize.
     */
    decay.velocity = [NSValue valueWithCGPoint:velocity];
    /**
     @abstract The deceleration factor.
     @discussion Values specifies should be in the range [0, 1].
                 Lower values results in faster deceleration. Defaults to 0.998.
     */
    decay.deceleration = 0.99;
    
    /**
     @abstract The original velocity value.
     @discussion Since the velocity property is modified as the animation progresses,
                 this property stores the original, passed in velocity to support autoreverse and repeatCount.
     */
    SLog(@"originalVelocity: %@", NSStringFromCGPoint([decay.originalVelocity CGPointValue]));
    
    /**
     @abstract The expected duration.
     @discussion Derived based on input velocity and deceleration values.
     */
    SLog(@"duration: %f", decay.duration);
    
    [self.blueBallView pop_addAnimation:decay forKey:@"decay"];
    
//    [sender setTranslation:CGPointZero inView:self.view];
}

@end
